package com.coocaa.x.app.framework.data;

import android.content.Context;
import android.content.res.AssetManager;
import android.util.Xml;

import com.coocaa.x.framework.app.CoocaaApplication;
import com.coocaa.x.framework.utils.LogUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

public class XMLParser {
    private static Context mContext = CoocaaApplication.getContext();

    public static Map<String, String> getAndroidAppConfig() {
        Map<String, String> androidAppConfig = new HashMap<String, String>();
        try {
            AssetManager am = mContext.getAssets();
            InputStream is = am.open("androidapp_config.xml");
            XmlPullParser xmlParser = Xml.newPullParser();
            try {
                xmlParser.setInput(is, "utf-8");
                int event = xmlParser.getEventType();
                while (event != XmlPullParser.END_DOCUMENT) {
                    switch (event) {
                        case XmlPullParser.START_DOCUMENT:
                            break;
                        case XmlPullParser.START_TAG:
                            if ("app".equals(xmlParser.getName())) {
                                String pkg = xmlParser.getAttributeValue(null, "package");
                                String value = xmlParser.getAttributeValue(null, "value");
                                LogUtils.d("pase", "getAndroidAppConfig  pkg value:" + pkg + " " + value);
                                androidAppConfig.put(pkg, value);
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            break;
                    }
                    event = xmlParser.next();
                }
                is.close();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return androidAppConfig;
    }

    public static Map<String, String> getNonDeleteAppConfig(String fpath) {
        Map<String, String> nonDeleteAppConfig = new HashMap<String, String>();
        try {
            InputStream is = new FileInputStream(fpath);
            XmlPullParser xmlParser = Xml.newPullParser();
            try {
                xmlParser.setInput(is, "utf-8");
                int event = xmlParser.getEventType();
                while (event != XmlPullParser.END_DOCUMENT) {
                    switch (event) {
                        case XmlPullParser.START_DOCUMENT:
                            break;
                        case XmlPullParser.START_TAG:
                            if ("config".equals(xmlParser.getName())) {
                                String pkg = xmlParser.getAttributeValue(null, "name");
                                String value = xmlParser.getAttributeValue(null, "value");
                                LogUtils.d("pase", "getAndroidAppConfig  pkg value:" + pkg + " " + value);
                                nonDeleteAppConfig.put(pkg, value);
                            }
                            break;
                        case XmlPullParser.END_TAG:
                            break;
                    }
                    event = xmlParser.next();
                }
                is.close();
            } catch (XmlPullParserException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return nonDeleteAppConfig;
    }

}
